package in.ac.iitb.cse.nlp.postagger.data;

import java.util.ArrayList;
import java.util.List;

public class BiTableCreator extends TableCreator{
	
	public String prevTagConf;
		
	public BiTableCreator()
	{
		
	}
	
	public String getPrevTagString()
	{
		return prevTag;
	}
	
	public void updateGram_Conf(String word, String currentTag)
	{
		if( currentTag.contains("-") == true )
		{
			String tag[] = currentTag.split("-");
			for(int i=0;i<2;i++)
			{
				tagSet.add(tag[i]); //add Tag to set
				transitionMatrix.updateCount(tag[i]); // Unigram count
				if( prevTag != null )
					transitionMatrix.update(prevTag ,tag[i]); // Bigram count
				if( prevTagConf != null )
					transitionMatrix.update(prevTagConf ,tag[i]); // Bigram count
				emissionMatrix.update(tag[i], word); // Emission Count
			}
			prevTag = tag[0];
			prevTagConf = tag[1];
		}
		else 
		{
			tagSet.add(currentTag); //add Tag to set
			transitionMatrix.updateCount(currentTag); // Unigram count
			if( prevTag != null )
				transitionMatrix.update(prevTag ,currentTag); // Bigram count
			if( prevTagConf != null )
				transitionMatrix.update(prevTagConf ,currentTag); // Bigram count
			emissionMatrix.update(currentTag, word); // Emission Count
			prevTag = currentTag;
			prevTagConf = null;			
		}
	}

	public void updateGram_Base(String word, String currentTag)
	{
		tagSet.add(currentTag); //add Tag to set
		transitionMatrix.updateCount(currentTag); // Unigram count
		if( prevTag != null )
		{
			transitionMatrix.update(prevTag ,currentTag); // Bigram count
		}
		// For updating emission table
		emissionMatrix.update(currentTag, word); // Emission Count
		prevTag = currentTag;
		prevTagConf = null;
	}

	public void updateGram(String word, String currentTag)
	{
		if( Config.isAmbigutyResolv == true )
			updateGram_Conf(word, currentTag);
		else
			updateGram_Base(word, currentTag);
	}
	public void beginLine()
	{
		String currentTag = "^";
		String word = "^";
		
		updateGram(word, currentTag);
	}
	
	public void endLine()
	{
		// only one $ tag at the end
		String currentTag = "$";
		String word = "$";
		updateGram(word, currentTag);
		prevTag = null; // Unset for new Sentence 
	}

	public void processLine(String line)
	{
		String[] split = line.split(WORD_SEPARATOR);
		for (String string : split) {
			String[] strings = string.split(TAG_SEPARATOR); //Dont assume start of the line is start of sentence
			if( strings.length >=2 )
			{
				if( prevTag ==null )
					beginLine();
				
				updateGram(strings[1], strings[0]);
				Config.nTrainWords++;
				
				/* End of Line */
				if (strings[1].equals(END_OF_SENTENCE) || 
						strings[1].equals(ALTERNATE_END_OF_SENTENCE)) {
					endLine();
				}
				
			}
		}
	}
	
	
	public List<String> tagSentence(String sentence)
	{
		String[] split = sentence.split(" ");
		List<String> input = new ArrayList<String>();
		for(String string : split)
			input.add(string);
		HMM vhmm = new BiViterbiHMM();
		vhmm.init(new ArrayList<String>(tagSet),transitionMatrix, emissionMatrix);
		List<String> bestProbTagSeq = vhmm.bestProbTagSeq(input);
		return bestProbTagSeq;
	}
	
}
